package com.jlboot.model.po.sys;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jlboot.core.id.UUIDHex;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangsanfei
 * @version 1.0
 * Create by 2024/1/29 21:17
 */
public class SysIdGenerator extends Model<SysIdGenerator> {

    public static final SysIdGenerator dao=new SysIdGenerator();
    public static final String table="sys_id_generator";

    private static Map<String,Object[]> bizMap=new HashMap<>();

    public SysIdGenerator(){
        this.set("id", UUIDHex.getUUIDHex().substring(0,20));
    }
    public String getNextId(String bizType){
        if(StrKit.isBlank(bizType)){
            throw new RuntimeException("bizType参数不能为空");
        }

        synchronized (bizType){
            Object[] max_id=bizMap.get(bizType);
            if(max_id==null){
                max_id=getMaxId(bizType);
            }
            Long nextId=(Long)max_id[0]+1;
            if(nextId>((Long)max_id[2]+(Integer)max_id[1])){
                max_id=getMaxId(bizType);
                nextId=(Long)max_id[0]+1;
            }
            max_id[0]=nextId;
            bizMap.put(bizType,max_id);
            //此框架，一版并发足够应对，这里的所有ID全部使用String，开发迅速，不用关心太多
            return nextId.toString();
        }

    }


    private Object[] getMaxId(String bizType){
        if(StrKit.isBlank(bizType)){
            throw new RuntimeException("bizType参数不能为空");
        }

        Object[] os=new Object[3];

        synchronized (bizType){
            Record sys_id_generator= Db.findFirst("select * from sys_id_generator where biz_type=?",bizType);
            if(sys_id_generator==null){
                sys_id_generator=new Record();
                sys_id_generator.set("id",System.currentTimeMillis()+""+(int)(Math.random()*100));
                sys_id_generator.set("del_tag",0);
                sys_id_generator.set("create_time",new Date());
                sys_id_generator.set("update_time",new Date());
                //初始ID 100000
                sys_id_generator.set("max_id",100000);
                sys_id_generator.set("version",0);
                sys_id_generator.set("biz_type",bizType);
                //步长默认1000
                sys_id_generator.set("step",1000);
                Db.save("sys_id_generator",sys_id_generator);
            }

            Long max_id=sys_id_generator.getLong("max_id");
            int version=sys_id_generator.getInt("version");
            int step=sys_id_generator.getInt("step");

            max_id=max_id+step;

            String sql="update sys_id_generator " +
                    "   set max_id = max_id+step " +
                    ", version = version + 1 " +
                    ", update_time = now() " +
                    " where version = ? and biz_type = ?";
            int result=Db.update(sql,version,bizType);
            if(result>0){
                os[0]=max_id;
                os[1]=step;
                os[2]=max_id;
            }else{
                throw new RuntimeException("系统异常");
            }

        }

        return os;
    }




}
